#coding:utf-8

from flask import Blueprint, session, request, redirect, url_for, render_template
from service.userservice import uservice
from service.orderservice import oservice
from model.userinfo import UserInfo

uc = Blueprint("usercontroller", __name__, template_folder="html")

@uc.route("/login", methods=["GET"])
def login_html():
	return render_template("login.html")

@uc.route("/out", methods=["GET"])
def out_html():
	session.pop("user", None)
	return render_template("login.html")

@uc.route("/login", methods=["POST"])
def login():
	username = request.values.get("username")
	password = request.values.get("password")

	user = uservice.get_by_name_pwd(username, password)
	if not user:
		return redirect(url_for("usercontroller.login"))

	session["user"] = user.uname
	res = oservice.get_all_today()
	return render_template("index.html", orderList = res, user=user)

